package com.config;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.alibaba.fastjson2.JSON;
import com.entity.OrmFileRes;
import com.entity.OrmInfoFile;
import com.entity.OrmJdbcFile;

public class OrmFile {
	public final static String ormpath = "C:" + File.separator + "orm" + File.separator + "orm.config";
	public final static String jdbcpath = "C:" + File.separator + "orm" + File.separator + "jdbc.config";


	public static OrmFileRes<OrmInfoFile> readOrm() {
		File file = new File(ormpath);
		try {
			String txt = FileUtils.readFileToString(file, "UTF-8");
			OrmInfoFile info = JSON.parseObject(txt, OrmInfoFile.class);
			return new OrmFileRes<OrmInfoFile>(1, "ok", info);
		} catch (IOException e) {
			return new OrmFileRes<OrmInfoFile>(0, "文件读取行数异常：" + e.getMessage());
		}
	}

	public static OrmFileRes<OrmJdbcFile> readJdbc() {
		File file = new File(jdbcpath);
		try {
			String txt = FileUtils.readFileToString(file, "UTF-8");
			OrmJdbcFile info = JSON.parseObject(txt, OrmJdbcFile.class);
			return new OrmFileRes<OrmJdbcFile>(1, "ok", info);
		} catch (IOException e) {
			return new OrmFileRes<OrmJdbcFile>(0, "文件读取行数异常：" + e.getMessage());
		}
	}

	public static OrmFileRes<String> writeOrm(OrmInfoFile txt) {
		File file = new File(ormpath);
		try {
			String jsonStr = JSON.toJSONString(txt);
			FileUtils.writeStringToFile(file, jsonStr, "UTF-8", false);
		} catch (IOException e) {
			return new OrmFileRes<String>(0, "文件写入异常：" + e.getMessage());
		}
		return new OrmFileRes<String>(1, "写入成功");
	}

	public static OrmFileRes<String> writeJdbc(OrmJdbcFile txt) {
		File file = new File(jdbcpath);
		try {
			String jsonStr = JSON.toJSONString(txt);
			FileUtils.writeStringToFile(file, jsonStr, "UTF-8", false);
		} catch (IOException e) {
			return new OrmFileRes<String>(0, "文件写入异常：" + e.getMessage());
		}
		return new OrmFileRes<String>(1, "写入成功");
	}
	
	public static void initOrmConfig(){
		File file = new File(ormpath);
		if (!file.exists()) {
			OrmInfoFile txt = new OrmInfoFile(
					"", 
					"", 
					"", 
					1,
					"test", 
					"sys_test", 
					"com.wymbkj.orm", 
					"com.wymbkj.domain", 
					"com.wymbkj.mapper", 
					"码农小鹿");
			writeOrm(txt);
		}
	}
	
	public static void initJdbcConfig(){
		File file = new File(jdbcpath);
		if (!file.exists()) {
			OrmJdbcFile txt = new OrmJdbcFile(
					"com.mysql.cj.jdbc.Driver", 
					"jdbc:mysql://192.168.0.248:3309", 
					"o-1.0", 
					"wymb", 
					"wymb@123456");
			writeJdbc(txt);
		}
	}

	public static void saveConfig(
			String entityPath,
			String daoPath,
			String mapperPath,
			int maven,
			String module,
			String table,
			String dao,
			String entity,
			String mapper,
			String author
			) {
		OrmInfoFile txt = new OrmInfoFile(
				entityPath, 
				daoPath, 
				mapperPath, 
				maven,
				module, 
				table, 
				dao, 
				entity, 
				mapper, 
				author);
		writeOrm(txt);
	}

	public static void saveJdbc(
			String driver, 
			String url, 
			String database, 
			String user, 
			String password) {
		OrmJdbcFile txt = new OrmJdbcFile(
				driver, 
				url, 
				database, 
				user, 
				password);
		writeJdbc(txt);
	}
}
